package com.zja.www.service.impl;

import com.zja.www.dao.AdminDao;
import com.zja.www.dao.DoctorDao;
import com.zja.www.dao.ScheduleDao;
import com.zja.www.dao.UserDao;

import com.zja.www.entity.Admin;
import com.zja.www.entity.Schedule;
import com.zja.www.entity.User;
import com.zja.www.service.AdminService;

import java.sql.Timestamp;
import java.util.List;

public class AdminServiceImpl implements AdminService {

    private AdminDao adminDao ;
    private UserDao userDao ;
    private DoctorDao doctorDao ;
    private ScheduleDao scheduleDao ;

    public AdminServiceImpl(AdminDao adminDao, UserDao userDao,
                            DoctorDao doctorDao, ScheduleDao scheduleDao) {
        this.adminDao = adminDao;
        this.userDao = userDao;
        this.doctorDao = doctorDao;
        this.scheduleDao = scheduleDao;
    }

    @Override
    public boolean login(String username, String password) {
        if (username == null || password == null ) {
            return false;
        }
        return adminDao.login(username, password);
    }

    @Override

    public boolean addAdmin(Admin admin) {
        if(admin == null ||admin.getUserName() == null){
            return false;
        }

        if(adminDao.getAdminByUsername(admin.getUserName()) != null){
            return false;
        }
        return adminDao.insert(admin);

    }

    @Override
    public Admin getAdminById(int id) {
        if(adminDao.getAdminById(id) != null){
            return adminDao.getAdminById(id);
        }
        return null;
    }

    @Override
    public Admin getAdminByUsername(String username) {
        if(adminDao.getAdminByUsername(username) != null){
            return adminDao.getAdminByUsername(username);
        }
        return null;
    }

    @Override
    public List<Admin> getAllAdmins() {
        if(adminDao.getAllAdmins() != null){
            return adminDao.getAllAdmins();
        }
        return null;
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        if(admin == null || adminDao.getAdminById(admin.getId()) == null){
            return false;
        }
        return adminDao.updateAdmin(admin);
    }

    @Override
    public boolean deleteAdmin(int id) {
        if(adminDao.getAdminById(id) == null){
            return false;
        }
        return adminDao.deleteAdmin(id);
    }


    @Override
    public boolean addSchedule(Schedule schedule) {
        if (schedule == null || schedule.getDoctorId() <= 0 ||
                schedule.getDate() == null) {
            return false;
        }
        return scheduleDao.insert(schedule);
    }

    @Override
    public boolean updateSchedule(Schedule schedule) {
        if (schedule == null || schedule.getId() <= 0) {
            return false;
        }
        return scheduleDao.update(schedule);
    }

    @Override
    public boolean deleteSchedule(int scheduleId) {
        return scheduleDao.delete(scheduleId);
    }

    @Override
    public Schedule getScheduleById(int scheduleId) {
        return scheduleDao.getById(scheduleId);
    }

    @Override
    public List<Schedule> getSchedulesByDate(Timestamp date) {
        return scheduleDao.getByDate(date);
    }

    @Override
    public List<Schedule> getSchedulesByDoctor(int doctorId) {
        return scheduleDao.getByDoctor(doctorId);
    }

    @Override
    public List<Schedule> getSchedulesByDepartment(int departmentId) {
        return scheduleDao.getByDepartment(departmentId);
    }

    @Override
    public List<User> getUnverifiedUsers() {
        return userDao.getUsersByStatus("unverified");
    }

    @Override
    public List<User> getVerifiedUsers() {
        return userDao.getUsersByStatus("verified");
    }

    @Override
    public boolean verifyUser(int userId) {
        User user = userDao.getUserById(userId);
        if (user != null) {
            user.setStatus("verified");
            return userDao.updateUser(user);
        }
        return false;
    }

    @Override
    public boolean unverifyUser(int userId) {
        User user = userDao.getUserById(userId);
        if (user != null) {
            user.setStatus("unverified");
            return userDao.updateUser(user);
        }
        return false;
    }






}
